public class Test {
    public static void main(String[] args) {

    }
    //59.螺旋矩阵
    public int[][] generateMatrix(int n) {
        int count = n/2;
        int[][] ret = new int[n][n];
        int k =1;//给矩阵赋值
        int i=0,j=0;//遍历矩阵，作为下标
        int start = 0;//while循环开始的起点
        while(count-- > 0) {
            //遍历上传 从左到右
            for(;j<n-start-1;j++)  ret[i][j] = k++;
            for(;i<n-start-1;i++)  ret[i][j] = k++;
            for(;j>start;j--)  ret[i][j] = k++;
            for(;i>start;i--)  ret[i][j] = k++;

            start++;
            i=start;
            j=start;
        }
        if(n % 2 != 0) ret[i][j] = k;
        return ret;
    }
    //122.买卖股票的最佳时机Ⅱ
    public int maxProfit(int[] prices) {
        int n = prices.length;
        if(n==1) return 0;
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = -prices[0];
        for(int i=1;i<n;i++) {
            f[i] = Math.max(f[i-1],g[i-1]-prices[i]);
            g[i] = Math.max(g[i-1],f[i-1]+prices[i]);
        }
        return g[n-1];
    }
    //309.买卖股票的最佳时机含冷冻期--四种状态写法
    public int maxProfit2(int[] prices) {
        int n = prices.length;
        if(n==1) return 0;
        int[][] dp = new int[n][4];
        dp[0][0] = -prices[0];
        for(int i=1;i<n;i++) {
            dp[i][0] = Math.max(dp[i-1][0],Math.max(dp[i-1][3]-prices[i],dp[i-1][1]-prices[i]));
            dp[i][1] = Math.max(dp[i-1][1],dp[i-1][3]);
            dp[i][2] = dp[i-1][0] + prices[i];
            dp[i][3] = dp[i-1][2];
        }
        int max = dp[n-1][0];
        for(int i=1;i<4;i++) {
            if(max < dp[n-1][i])
                max = dp[n-1][i];
        }
        return max;
    }


}
